/*
 * Created on 29/09/2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.davinci.freemarket.businessLayer.utils.writer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

//import ml.libweb.LibTxt;
//import ml.libweb.MLWriter;


/**
 *
 */
public abstract class FreeMarketAbstractWriter implements FreeMarketWriter{
	
	private static final String CRLF = "\n";
	private static final String DEFAULT_CHARSET = "ISO-8859-1";
	
	protected int size;
	protected ByteArrayOutputStream byteOutPut;
	
	protected String charset;
	protected StringBuilder out = new StringBuilder(100000);
	
	public FreeMarketAbstractWriter(String charset){
		
		if("".equals(charset))this.charset = DEFAULT_CHARSET;
			else this.charset = charset; 
	}
	
	public int size(){
		return this.size;
	}
	
	public void flush(OutputStream outStream) throws IOException {
		
		this.byteOutPut.writeTo(outStream);
		this.byteOutPut.flush();
		outStream.flush();
		outStream.close();
		
		this.byteOutPut = null;
		this.out = null;
	}
	
	public void print(String txt) {
		this.out.append(txt);
	}

	public void print(StringBuffer txt) {
		this.out.append(txt);
	}

	public void println(Object txt) {
		this.out.append((String)txt);
		this.out.append(CRLF);
	}

	public void println(String txt) {
		this.out.append(txt);
		this.out.append(CRLF);
	}

	public void println(StringBuffer txt){
		this.out.append(txt);
		this.out.append(CRLF);
	}
	
	public void delete(){
		if (this.out != null && this.out.length() > 0)
			this.out.delete(0, this.out.length()-1);
	}
	
	public void replace(String find, String replace) {
//		LibTxt.replace(this.out, find, replace);
	}
	
	public void replaceAll(String find, String replace) {
//		LibTxt.replaceAll(this.out, find, replace);
	}
	
	public void replace(String find, Object replace) {
//		LibTxt.replace(this.out, find, replace.toString());
	}
	
	public void replaceAll(String find, Object replace) {
//		LibTxt.replaceAll(this.out, find, replace.toString());
	}

	public String toString() {
		return this.out.toString();
	}

	public void finalize() throws Throwable {
		super.finalize();
		if ( this.out != null )
			this.out = null;
	}
	
}
